package com.xfcycle.zebrabikegp.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.xfcycle.zebrabikegp.R;
import com.xfcycle.zebrabikegp.constant.ToastConstant;

import java.util.List;

public class GoogleMapActivity extends FragmentActivity implements OnMapReadyCallback {

    public GoogleMap mMap;
    private LocationManager locationManager;
    private Location myLocation;
    private Double latitude;
    private Double longitude;

    /**
     * 地图回调方法
     *
     * @param googleMap
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        //确定已获得权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
        }
        //获得位置信息提供者
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        //开启GPS
        openGPSSettings();

        //获得定位信息
        for (String provider : locationManager.getProviders(true)) {
            myLocation = locationManager.getLastKnownLocation(provider);
            if(null != myLocation){
                break;
            }
        }
        mMap = googleMap;
        //定义获取自身位置小图标
        mMap.setMyLocationEnabled(true);

        if(null == myLocation){
            LatLng initLatlng = new LatLng(32.05, 118.8);
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(initLatlng,12));
            return;
        }
        //经度
        longitude = myLocation.getLongitude();
        //纬度
        latitude = myLocation.getLatitude();

        //设置程序启动时经纬度定位,先维度，后经度
        LatLng mylatlng = new LatLng(latitude, longitude);
        //设置摄像头位置，及地图比列尺大小 数字越小 单屏显示的范围越大
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mylatlng,14));

        //添加自定义图标点击事件
        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {

                return false;
            }
        });

    }

    //在地图上添加一个车的标识
    public Marker addBikeMarkerToMap(String x,String y,String title){
        //设置经纬度数据
        LatLng bikeLocation = new LatLng(Double.parseDouble(x),Double.parseDouble(y));

        //添加地图标识，并返回
        Marker bikeMarker = mMap.addMarker(new MarkerOptions().position(bikeLocation).title(title).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_bike_marker_find)));

        return bikeMarker;
    }

    /**
     * 获取位置提供者对象
     */
    public List<String> getProvider() {
        //构建查询条件
        Criteria criteria = new Criteria();
        //查询精度：高
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        //是否查询海拔:否
        criteria.setAltitudeRequired(false);
        //是否允许产生开销
        criteria.setCostAllowed(true);
        //设置获取频率
        criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
        //返回最适合条件的Provider，第二个参数为true说明，如果只有一个provider，则直接提供
        return locationManager.getProviders(true);
    }

    /**
     * 判断GPS定位是否打开，如果没打开，则跳转到GPS设置页面
     */
    public void openGPSSettings() {

        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(this.getApplicationContext(), ToastConstant.OPEN_GPS, Toast.LENGTH_SHORT).show();
            //跳转到打开页面
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(intent,0);
        }
    }

}

